﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using VivendoByteToolkit.Controls;
using VivendoByteToolkit.Enums;

namespace VivendoByteToolkit.ViewModels
{
    public class NumberPickerViewModel : ViewModelBase
    {
        #region Public properties
        public NumberPicker NumberPicker { get; set; }

        private string number;
        public string Number
        {
            get
            {
                return number;
            }
            set
            {
                number = value;
                base.RaisePropertyChanged("Number");
            }
        }

        private bool keyboardEnabled;
        public bool KeyboardEnabled
        {
            get
            {
                return keyboardEnabled;
            }
            set
            {
                keyboardEnabled = value;
                base.RaisePropertyChanged("KeyboardEnabled");
            }
        }
        #endregion

        #region Command
        public ICommand IncrementCommand { get; set; }
        public ICommand DecrementCommand { get; set; }
        public ICommand EnableKeyboardCommand { get; set; }
        #endregion

        #region ctor
        public NumberPickerViewModel()
        {
            this.Number = "0";

            this.IncrementCommand = new RelayCommand(IncrementCommandExecute);
            this.DecrementCommand = new RelayCommand(DecrementCommandExecute);
            this.EnableKeyboardCommand = new RelayCommand(EnableKeyboardCommandExecute);
        }
        #endregion

        #region Number management
        private void IncrementCommandExecute()
        {
            int newValue = (Int32.Parse(this.Number));
            newValue++;

            if (newValue > NumberPicker.GetMaximum(this.NumberPicker))
            {
                this.Number = NumberPicker.GetMaximum(this.NumberPicker).ToString();
            }
            else
            {
                this.Number = newValue.ToString();
            }

            this.UpdateNumber();
        }

        private void DecrementCommandExecute()
        {
            var newValue = (Int32.Parse(this.Number));
            newValue--;

            if (newValue < NumberPicker.GetMinimum(this.NumberPicker))
            {
                this.Number = NumberPicker.GetMinimum(this.NumberPicker).ToString();
            }
            else
            {
                this.Number = newValue.ToString();
            }

            this.UpdateNumber();
        }
        #endregion

        private void EnableKeyboardCommandExecute()
        {
            this.KeyboardEnabled = !this.KeyboardEnabled;
        }

        private void UpdateNumber()
        {
            this.UpdateDependencyProperty();
        }

        private void UpdateDependencyProperty()
        {
            if (this.NumberPicker != null)
            {
                this.NumberPicker.SetValue(NumberPicker.NumberProperty, this.Number);
            }
        }
    }
}